package cn.jinshengda.service.impl;

import cn.jinshengda.Dto.UsernameAndPassword;
import cn.jinshengda.entity.Admin;
import cn.jinshengda.mapper.AdminMapper;
import cn.jinshengda.service.AdminService;
import cn.jinshengda.utils.StringUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-01-08
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /*
     * @Author Lxy
     * @Description //TODO 登录功能
     * @Param admin 前端传来的用户名及密码
     * @return map 返回登录的对象
    */
    @Override
    public Map<String, Object> login(UsernameAndPassword uap) {
        Map<String,Object> map=new HashMap<>();
        QueryWrapper<Admin> wrapper=new QueryWrapper<>();
        wrapper.eq("username",uap.getUsername());
        Admin admin = adminMapper.selectOne(wrapper);
        String encode = StringUtil.MD5Encode(uap.getPassword());
        System.out.println(encode+"---------------------------------");
     //   String s= DigestUtils.md5DigestAsHex(uap.getPassword().getBytes(StandardCharsets.UTF_8));

        if(admin!=null){
            if(encode.equals(admin.getPassword())){
                map.put("data",admin);
                map.put("status","成功");
            }else{
                map.put("status","密码错误");
            }
        }else{
            map.put("status","用户名不存在");
        }
        return map;
    }
}
